import java.util.Stack;

public class Solution1021 {
    public String removeOuterParentheses(String s) {
        Stack<Character> stack=new Stack<>();
        StringBuilder stringBuilder=new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c=='('){
                if (!stack.isEmpty()){
                    stringBuilder.append('(');
                }
                stack.push('(');
            }else{
                stack.pop();
                if (!stack.isEmpty()){
                    stringBuilder.append(')');
                }
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution1021().removeOuterParentheses("(()())(())(()(()))"));
    }
}
